package com.webdrider.model.validators;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.security.abstracts.AbstractError;
import com.common.security.exceptions.ManagerException;
import com.webdrider.manager.action.snippet.DomainManager;
import com.webdrider.model.action.snippet.Domain;
import com.webdrider.model.requests.snippet.AddSnippetDomainRequest;
import com.webdrider.security.errors.SnippetError;

public class SnippetDomainUnicityValidator extends AbstractValidator {

	@Override
	public boolean validate(AbstractRequest abstractRequest) {
		AddSnippetDomainRequest request = null;
		
		if (abstractRequest instanceof AddSnippetDomainRequest) {
			request = (AddSnippetDomainRequest) abstractRequest;
		} else {
			error = new SnippetError(AbstractError.INVALID_REQUEST);
			return false;
		}
		
		try {
			Domain domain = request.getDomain();
			DomainManager manager = new DomainManager();
			boolean exist = manager.exist(domain.getValue());
			
			if (exist == true) {
				error = new SnippetError(SnippetError.DOMAIN_EXISTS);
				return false;
			}
			
			return true;
		} catch (ManagerException e) {
			error = new SnippetError(AbstractError.DATABASE_ERROR);
			return false;
		}
		
	}

}
